package com.tm.action.feixin.impl;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.Consts;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.google.gson.Gson;
import com.tm.action.feixin.BaseAction;
import com.tm.action.feixin.HttpCallBack;
import com.tm.model.BaseModel;
import com.tm.model.PersonInfo;
import com.tm.util.Constants;
import com.tm.util.CookieUtils;

@Controller
@RequestMapping("/fetion")
public class FetionAction extends BaseAction {
	
	@RequestMapping(method = RequestMethod.GET)
	public String index(){
		return "login";
	}

	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public String signIn(final HttpServletRequest request,
			final HttpServletResponse resp,
			@RequestParam("username") final String username,
			@RequestParam("password") final String password) {
		return executeCallBack(new HttpCallBack() {
			
			@Override
			public String execute() {
				// get method
				DefaultHttpClient httpclient = new DefaultHttpClient();
				HttpPost postMethod = getPostMethod(Constants.URL_LOGIN);
				// ready data: parameters
				List <NameValuePair> nvps = new ArrayList <NameValuePair>();
		        nvps.add(new BasicNameValuePair("UserName", username));
		        nvps.add(new BasicNameValuePair("Pwd", password));
		        nvps.add(new BasicNameValuePair("OnlineStatus", "400"));
		        nvps.add(new BasicNameValuePair("AccountType", "1"));
		        nvps.add(new BasicNameValuePair("Version", "1"));
		        postMethod.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));
				// execute method
		        String view = executeSignIn(httpclient, resp, postMethod);
		        
		        return view;
				// return msg

			}
		});
		
	}
	
	@RequestMapping(value = "/getPersonInfo", method = RequestMethod.GET)
	public String getPersonInfo (final HttpServletRequest request, final HttpServletResponse resp) {
		
		PersonInfo personInfo = new PersonInfo();
		
		String returnValue = executeCallBack(new HttpCallBack() {
			
			@Override
			public String execute() {
				DefaultHttpClient httpclient = new DefaultHttpClient();
				HttpPost postMethod = getPostMethod(Constants.URL_GET_PERSON_INFO + nextRequestVersion());
				String ssid = CookieUtils.getCookie(request, "webim_sessionid");
				
				// ready data: parameters
				List <NameValuePair> nvps = new ArrayList <NameValuePair>();
				nvps.add(new BasicNameValuePair("ssid", ssid));
				postMethod.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));
				postMethod = setHeaders(request, postMethod);
				// execute method
				String returnValue = executePostMethod(httpclient, postMethod);
				
				return returnValue;
				
			}
		});
		
		Gson gson = new Gson();
		personInfo = gson.fromJson(returnValue, PersonInfo.class);
        System.out.println(personInfo);
        if (personInfo != null) {
        	if (personInfo.getRc() == Constants.STATUS_SUCCESS) {
        		CookieUtils.addCookie(resp, Constants.FETION_ID, personInfo.getRv().getUid());
		        
        	}
        }
        
        return getViewByStatus(personInfo.getRc());
	}
	
	@RequestMapping(value = "/getContactList", method = RequestMethod.GET)
	public String getContactList (final HttpServletRequest request) {
		
		return executeCallBack(new HttpCallBack() {
			
			@Override
			public String execute() {
				DefaultHttpClient httpclient = new DefaultHttpClient();
				HttpPost postMethod = getPostMethod(Constants.URL_GET_CONTACT_LIST + nextRequestVersion());
				String ssid = CookieUtils.getCookie(request, "webim_sessionid");
				
				// ready data: parameters
				List <NameValuePair> nvps = new ArrayList <NameValuePair>();
				nvps.add(new BasicNameValuePair("ssid", ssid));
				postMethod.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));
				postMethod = setHeaders(request, postMethod);
				// execute method
				String view = executePostMethod(httpclient, postMethod);
				return view;
				
			}
		});
		
	}
	
	@RequestMapping(value = "/sendMsg", method = RequestMethod.POST)
	public String sendMsg (final HttpServletRequest request,
			@RequestParam("message") final String message,
			@RequestParam("receivers") final String receivers) {
		
		String returnValue = executeCallBack(new HttpCallBack() {
			
			@Override
			public String execute() {
				DefaultHttpClient httpclient = new DefaultHttpClient();
				HttpPost postMethod = getPostMethod(Constants.URL_SEND_CONTENT_MSG + nextRequestVersion());
				String ssid = CookieUtils.getCookie(request, "webim_sessionid");
				String fetionId = CookieUtils.getCookie(request, Constants.FETION_ID);
//				// ready data: parameters
				List <NameValuePair> nvps = new ArrayList <NameValuePair>();
				nvps.add(new BasicNameValuePair("UserName", fetionId));
		        nvps.add(new BasicNameValuePair("Message", message));
		        nvps.add(new BasicNameValuePair("Receivers", receivers));
		        nvps.add(new BasicNameValuePair("ssid", ssid));
		        postMethod.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));
		        postMethod = setHeaders(request, postMethod);
				// execute method
		        String returnValue = executePostMethod(httpclient, postMethod);
		        return returnValue;
				
			}
		});
		

		Gson gson = new Gson();
		BaseModel msg = gson.fromJson(returnValue, BaseModel.class);
        System.out.println(msg);
        
        return getViewByStatus(msg.getRc());
		
	}
	

	
}
